let requestUrl;

function addHandler() {
	let roleId = $("#role_id").combogrid("getValue");
	let categoryId = $("#category_id").combobox("getValue");

	if (roleId) {
		$("#roleId").combogrid("setValue", roleId);
	}
	if (categoryId) {
		$("#categoryId").combobox("setValue", categoryId);
	}

	requestUrl = "/star_stone/insert";
	
	$("#starstone_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#starstone_list").datagrid("getSelected");
	
	if (rowData) {
		requestUrl = "/star_stone/updateById";

		$("#id").val(rowData.id);
		$("#state").combobox("setValue", rowData.state);
		$("#days").numberspinner("setValue", rowData.days);
		$("#roleId").combogrid("setValue", rowData.roleId);
		$("#quality").numberspinner("setValue", rowData.quality);
		$("#categoryId").combobox("setValue", rowData.categoryId);

		$("#starstone_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function deleteHandler() {
	let rowData = $("#starstone_list").datagrid("getSelected");

	if (rowData) {
		$.messager.confirm("提示", "是否确认删除？", function(bool) {
			if (bool) {
				ajaxPost("/star_stone/deleteById", {
					id: rowData.id
				}, function(resp) {
					showMsg(resp.message);

					$("#starstone_list").datagrid("reload");
				}, error);
			}
		});
	} else {
		alertMsg("请选择要删除的记录！");
	}
}

$(document).ready(function() {

	$("#role_id").combobox({
		url: "/role_account/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "--选择角色--",
		formatter: function(row) {
			ajaxGet("/role_category/selectById", {
				id: row.categoryId
			}, function(resp) {
				let data = resp.data;

				$("#_role_" + row.id).attr("src", data.image);
			}, error);

			return "<img height='20' id='_role_" + row.id + "' />&nbsp;" + row.name;
		}
	});

	$("#category_id").combobox({
		url: "/star_stone_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "--选择晶石类型--",
		formatter: function(row) {
			return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let roleId = $("#role_id").combobox("getValue");
		let categoryId = $("#category_id").combobox("getValue");

		$("#starstone_list").datagrid("load", {
			roleId: roleId,
			categoryId: categoryId
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#categoryId").combobox({
        url: "/star_stone_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			return "<img height='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
    });
	
	$("#quality").numberspinner({
        min: 1,
        max: 100,
        width: 150,
        required: true
    });

	$("#state").combobox({
		url: "/bind_state/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto",
		onSelect: function (record) {
			let days = record.days;

			if (days <= 0) {
				$("#days").numberspinner("setValue", days);
			}
		}
	});

	$("#days").numberspinner({
		min: -1,
		max: 60,
		width: 150,
		required: true
	});
	
	$("#roleId").combogrid({
		url: "/role_account/selectAll",
		idField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelWidth: 440,
		panelHeight: "auto",
		columns: [[
			{field: "id", title: "角色ID", align: "center", width: 100},
			{field: "name", title: "角色名", align: "center", width: 120},
			{field: "categoryId", title: "角色造型", align: "center", width: 70
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/role_category/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#modeling_" + rowIndex).attr("src", data.image)
							.attr("title", data.name);
					}, error);

					return "<img height='32' id='modeling_" + rowIndex + "' />";
				}
			},
			{field: "grade", title: "等级", align: "center", width: 50},
			{field: "schoolId", title: "门派", align:"center", width: 50
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/school/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#school_" + rowIndex).attr("src", data.image)
							.attr("title", data.name);
					}, error);

					return "<img height='32' id='school_" + rowIndex + "' />";
				}
			}
		]]
	});
	
	$("#starstone_dialog").dialog({
		title: " ",
		width: 500,
		height: 300,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#starstone_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#starstone_dialog").dialog("close");
						$("#starstone_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#starstone_dialog").dialog("close");
				$("#starstone_form").form("clear");
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_role_details * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#starstone_list").datagrid({
		url: "/star_stone/selectByPage",
		height: height_table,
		rownumbers: true,
		fitColumns: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-delete",
			text: "删除",
			handler: function() {
				deleteHandler();
			}
		}],
		columns: [[
			{field: "id", title: "编号", align: "center", hidden: true},
			{field: "categoryId", title: "晶石类型", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/star_stone_category/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#starstone_" + rowIndex).attr({
							"src": data.image,
							"title": data.name
						});
					}, error);
					
					return "<img height='" + size + "' id='starstone_" + rowIndex + "' />";
				}
			},
			{field: "days", title: "冻结天数", align: "center", width: 100, sortable: true
				, formatter: function(value) {
					if (value === -1) {
						return "<div class='red'>摆摊永久冻结</div>";
					} else if (value === 0) {
						return "<div>/</div";
					} else {
						return "冻结<span class='red'>" + value + "</span>天";
					}
				}
			},
			{field: "state", title: "物品状态", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					if (!value) {
						return "<div> 无</div>";
					} else {
						ajaxGet("/bind_state/selectById", {
							id: value
						},function(resp) {
							let data = resp.data;

							$("#state_" + rowIndex).html(data.name);
						}, error);

						return "<div id='state_" + rowIndex + "'></div>";
					}
				}
			},
			{field: "quality", title: "品质", align: "center", width: 100, sortable: true},
			roleDetailsColumn
		]]
	});

});